package com.hangzhou.xyj.engine;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Handler;
import android.util.Log;

import com.hangzhou.xyj.event.CacheProgressEvent;
import com.hangzhou.xyj.event.PlayProgressEvent;
import com.hangzhou.xyj.model.type.EPlayControlType;
import com.hangzhou.xyj.utils.EventBusUtil;
import com.hangzhou.xyj.utils.constants.Constants;


/**
 * 播放器
 */
public class Player implements OnBufferingUpdateListener, OnCompletionListener,
		OnPreparedListener {
	private final String TAG = Player.class.getSimpleName();

	private static final int UPDATE_MSG = 0; //更新播放进度消息
	private int mTimeCount; //时间计数器

	public MediaPlayer mediaPlayer; 

	public Player() {
		super();
		try {
			mediaPlayer = new MediaPlayer();
			mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);// ?????????????
			mediaPlayer.setOnBufferingUpdateListener(this);
			mediaPlayer.setOnPreparedListener(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			int position = mediaPlayer.getCurrentPosition();
			int duration = mediaPlayer.getDuration();
			if (duration > 0) {
//				long pos = seekBar.getMax() * position / duration;
//				seekBar.setProgress((int) pos);
				PlayProgressEvent event = new PlayProgressEvent(position, duration,mTimeCount++);
				EventBusUtil.postEvent(event);
			}
			mHandler.sendEmptyMessageDelayed(UPDATE_MSG,1000); // 发送消息
		};
	};

	public void play() {
		mediaPlayer.start();
	}

	/**
	 * 
	 * @param url
	 * 播放的url
	 */
	public void playUrl(String url) {
		try {
			mediaPlayer.reset();
			mediaPlayer.setDataSource(url); 
			mediaPlayer.prepare(); 
			Constants.PLAY_STATE = EPlayControlType.PLAY;
			//每隔1s发送，用来更新播放进度
			mHandler.removeMessages(UPDATE_MSG);
			// 每一秒触发一次
			mHandler.sendEmptyMessage(UPDATE_MSG);
		} catch (Exception e) {
			e.printStackTrace();
			Constants.PLAY_STATE = EPlayControlType.EXIT;
			mHandler.removeMessages(UPDATE_MSG);
		}

//		catch (IllegalArgumentException e) {
//			e.printStackTrace();
//		} catch (SecurityException e) {
//			e.printStackTrace();
//		} catch (IllegalStateException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
	}

	// 
	public void pause() {
		if (mediaPlayer.isPlaying()) {
			mediaPlayer.pause();
			Constants.PLAY_STATE = EPlayControlType.PAUSE;
			mHandler.removeMessages(UPDATE_MSG);
		}
	}

	// 
	public void stop() {
		if (mediaPlayer != null) {
			mediaPlayer.stop();
			mediaPlayer.release();
			mediaPlayer = null;
		}
		Constants.PLAY_STATE = EPlayControlType.STOP;
		mHandler.removeMessages(UPDATE_MSG);
	}

	public void exit() {
		mediaPlayer.stop();
		mediaPlayer.release();
		mHandler.removeMessages(UPDATE_MSG);
	}

	@Override
	public void onPrepared(MediaPlayer mp) {
		mp.start();
		Log.e("mediaPlayer", "onPrepared");
	}

	@Override
	public void onCompletion(MediaPlayer mp) {
		mHandler.removeMessages(UPDATE_MSG);
		Log.e("mediaPlayer", "onCompletion");
	}

	/**
	 * 缓冲更新
	 * @param mp
	 * @param progress 缓存进度,用来设置seekBar的进度
	 */
	@Override
	public void onBufferingUpdate(MediaPlayer mp, int progress) {
		CacheProgressEvent event = new CacheProgressEvent(progress);
		EventBusUtil.postEvent(event);
	}

}
